﻿Namespace PL.INVSDK
    Public Class InventoryBatchs_PL
        Inherits PLBase
        Implements IPL, IPLProcess

        Public Property InvBatchNoSEQ As Boolean = True

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub
        Public Sub New()
            MyBase.New()
        End Sub

        Public Overrides Function Delete() As Boolean
            Return True
        End Function

        ''' <summary>
        ''' A chaque ligne on passe par ici pour les CUD. 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks>Attention ici le handle pourrait être un autre table</remarks>
        Public Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            'OutstandingIssues: Valider quelle table du dataset on traite.
            If e.StatementType = StatementType.Insert Or e.StatementType = StatementType.Update Then
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@InvBatchNoSEQ").Value = InvBatchNoSEQ
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@InvBatchNoSEQ").Value = False
            End If
        End Sub
        Public Overrides Function Validate() As Boolean
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)
            If IsDBNull(row("InvBatchCD")) Then
                row.SetColumnError("InvBatchCD", "Mandatory")
            End If
            If IsDBNull(row("Batch_DT")) Then
                row.SetColumnError("Batch_DT", "Mandatory")
            End If
            If IsDBNull(row("InvBatchSequence")) And Not InvBatchNoSEQ Then
                row.SetColumnError("InvBatchSequence", "Mandatory")
            End If
            If IsDBNull(row("StatusID_FK")) Then
                row.SetColumnError("StatusID_FK", "Mandatory")
            End If
            If IsDBNull(row("BatchTypeID_FK")) Then
                row.SetColumnError("BatchTypeID_FK", "Mandatory")
            End If
            If IsDBNull(row("BatchTranLines")) Then
                row.SetColumnError("BatchTranLines", "Mandatory")
            End If
            If IsDBNull(row("BatchTranAmt")) Then
                row.SetColumnError("BatchTranAmt", "Mandatory")
            End If
            If IsDBNull(row("Comment")) Then
                row.SetColumnError("Comment", "Mandatory")
            End If
            '
            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Function BatchRecords(ByVal dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(ByVal dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(ByVal dataRow As DataRow) As Boolean Implements IPLProcess.NewRecord
            dataRow("InvBatchID") = Guid.NewGuid
            dataRow("BatchTranAmt") = 0.0
            dataRow("BatchTranLines") = 0
            dataRow("Batch_DT") = Date.Now
            dataRow("StatusID_FK") = IdRepository.APP_TRANSTATUS_OPEN
            dataRow("BatchTypeID_FK") = IdRepository.APP_BATCHTYPE_STD
            dataRow("BatchClassId_FK") = IdRepository.APP_BATCHCLASS_INVT
            Return True
        End Function

        Public Shared Function ProcessInventoryBatch(ByVal invBatchID As Guid) As Boolean
            Dim sqlcmd As SqlCommand = Nothing
            '
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_ProcessInventoryBatchs")
                sqlcmd.Parameters("@InvBatchID").Value = invBatchID
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
            Return True
        End Function

        Public Shared Function SetInventoryBatchStatus(ByVal invBatchID As Guid, ByVal fromStatus As Guid, ByVal toStatus As Guid) As Boolean
            Dim sqlcmd As SqlCommand = Nothing
            '
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_InventoryBatch_Change_Status")
                sqlcmd.Parameters("@BatchID").Value = invBatchID
                sqlcmd.Parameters("@FromStatusID").Value = fromStatus
                sqlcmd.Parameters("@ToStatusID").Value = toStatus
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
            Return True
        End Function

        Public Shared Function SetTransactionStatus(ByVal invBatchID As Guid, ByVal fromStatus As Guid, ByVal toStatus As Guid) As Boolean
            Dim sqlcmd As SqlCommand = Nothing
            '
            Try

                sqlcmd = MetaQuery.GetSPCmdByMetaName("bsp_LotTransaction_Change_Status")
                sqlcmd.Parameters("@LotTranID").Value = DBNull.Value
                sqlcmd.Parameters("@InvBatchID_FK").Value = invBatchID
                sqlcmd.Parameters("@FromStatusID").Value = fromStatus
                sqlcmd.Parameters("@ToStatusID").Value = toStatus

                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
            Return True
        End Function
    End Class
End Namespace

